<?php

namespace zhtfrontend\models\datas;

use Yii;

/**
 * This is the model class for table "{{%goods_attr}}".
 *
 * @property integer $id
 * @property integer $goods_id
 * @property integer $attribute_id
 * @property integer $value_id
 * @property string $value
 * @property string $obj_type
 */
class GoodsAttr extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods_attr}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_id', 'attribute_id', 'value'], 'integer'],
            [['value', 'obj_type'], 'string', 'max' => 20],
            [['name'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增编号'),
            'goods_id' => Yii::t('zhtfrontend', '商品id'),
            'attribute_id' => Yii::t('zhtfrontend', '属性id'),
            'name' => Yii::t('zhtfrontend', '属相名称'),
            'value' => Yii::t('zhtfrontend', '属性值'),
            'obj_type' => Yii::t('zhtfrontend', '对象类型(teacher/cousre/time/classroom)'),
        ];
    }

    public static function InitParam($goods_id){
         $attrs = self::find()->where(['goods_id'=>$goods_id])->asArray()->all();
         $arr = [];
        foreach($attrs as $key=>$attr){
                $arr[$attr['obj_type']] = $attr['value'];
        }
        return $arr;
    }

    public function getGoods(){
        return $this->hasOne(Goods::className(),['id'=>'goods_id'])->where(['is_delete'=>0]);
    }
}
